<template>
  <div class="app-container">
    <el-card class="box-card search-div">
      <el-form :inline="true">
        <el-form-item label="名称">
          <el-input placeholder="请输入查询的名称" clearable v-model="listQuery.name" />
        </el-form-item>
        <el-button type="primary" @click="handleSearch" icon="el-icon-search">搜索</el-button>
        <el-button @click="handleReset" icon="el-icon-refresh">重置</el-button>
      </el-form>
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" v-if="permissions.sys_role_add" class="el-icon-plus" @click="add">新增</el-button>
        <el-button style="float: right;" icon="el-icon-refresh" circle @click="getList"></el-button>
      </div>
        <el-table :data="list" v-loading.body="listLoading" border fit highlight-current-row class="table-list">
          <el-table-column label="序号" type="index" min-width="5%"/>
          <el-table-column  align="center" width="120" label="名称" prop="name" >
          </el-table-column>
          <el-table-column align="center" width="160" label="驱动类" prop="driverClass" >
          </el-table-column>
          <el-table-column align="center" label="连接地址" prop="url" :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column align="center" width="80" label="用户名"  prop="username">
          </el-table-column>
          <el-table-column align="center" width="300"  label="备注" prop="remark" :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column align="center" label="操作" width="180" fixed="right">
            <template slot-scope="scope">
              <el-button plain v-if="permissions.sys_role_edit" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button plain v-if="permissions.sys_role_remove" type="danger" @click="removeByIds(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-container">
          <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
            :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
    </el-card>
    <!--弹出框-->
    <el-dialog :title="dialogTitle" :width="'650px'" :visible.sync="showDialog" :close-on-click-modal="false">
      <el-form :rules="rules" ref="saveDatasourceData" :model="saveDatasourceData" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称:" prop="name">
              <el-input v-model="saveDatasourceData.name" :maxlength="40"  clearable placeholder="最多输入20个字符"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="驱动类:" prop="driverClass">
              <el-select v-model="saveDatasourceData.driverClass" style="width: 100%;">
                <el-option label="com.mysql.cj.jdbc.Driver" value="com.mysql.cj.jdbc.Driver"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名:" prop="username">
              <el-input v-model="saveDatasourceData.username" :maxlength="40" clearable placeholder="最多输入40个字符"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码:" prop="password">
              <el-input type="password" v-model="saveDatasourceData.password" :maxlength="40" clearable placeholder="最多输入40个字符"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="连接地址:" prop="url">
          <el-input type="textarea" rows="3" v-model="saveDatasourceData.url" :maxlength="200" clearable placeholder="最多输入200个字符"></el-input>
        </el-form-item>
        <el-form-item label="描述:" prop="remark">
          <el-input type="textarea" rows="3" v-model="saveDatasourceData.remark" :maxlength="200" clearable placeholder="最多输入200个字符"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取 消</el-button>
        <el-button type="primary" @click="onSave('saveDatasourceData')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as datasource from './js/datasource'
import { mapGetters } from 'vuex'

export default {
  name: 'dev_datasource_list',
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1, limit: 10, name: ''
      },
      showDialog: false,
      saveDatasourceData: {id:'',name:'',driverClass:'com.mysql.cj.jdbc.Driver',username:'',password:'',url:'',remark:''},
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
        ,driverClass: [{required: true}]
        ,username: [{ required: true, message: '请输入用户名', trigger: 'blur' }]
        ,password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
        ,url: [{ required: true, message: '请输入连接地址', trigger: 'blur' }]
      },
      dialogTitle: '添加【数据源配置】'
    }
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      datasource.list(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleReset(){
      this.listQuery= {
        page: 1, limit: 10
      },
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    //删除
    removeByIds(id) {
      this.$confirm('请确定是否删除该数据', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        datasource.removeByIds(id).then((response) => {
          this.$message({ type: 'success', message: '操作成功!' })
          this.getList()
        })
      }).catch(function(error) { console.log(error) })
    },
    // 新增
    add(pit, index) {
      this.showDialog = true
      this.dialogTitle = '添加【数据源配置】'
      // 清空表单数据
      this.$nextTick(() => {
        this.resetForm('saveDatasourceData')
      })
    },
    // 编辑
    handleEdit(index, row) {
      this.showDialog = true
      this.dialogTitle = '编辑【数据源配置】'
      this.$nextTick(() => {
        this.resetForm('saveDatasourceData')
        this.saveDatasourceData = row
      })
    },
    // 重置表单 解决验证提示的问题
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 保存
    onSave(formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) {
          return false
        }
        this.showDialog = false
        datasource.submit(this.saveDatasourceData).then((response) => {
          this.$message({ type: 'success', message: '操作成功!' })
          this.getList()
        }).catch(function(error) {
          console.log(error)
        })
      })
    }
  }
}
</script>
